<?php
class control_index extends core_action{
	
	function index(){
		if($this->isAdminLogin() === false){
			$this->redirict(Qtpl::createUrl('index', 'login',array(),'admin'));
		}
		$model = new core_model_bug();
		$model->setLimit(5);
		//需要处理的最新5条
		$params['new'] = $model->select("is_sure=0 and is_ignore=0","","","order by bid desc")->items;
		$ymd=date('Y-m-d',strtotime('-30 day'));
		//需要公开的最新5条
		$params['open'] = $model->select("(is_sure=1 or is_ignore=1) and is_open=0 and create_ymd<'{$ymd}'","","","order by bid desc")->items;
		$this->render('simpla/index.php',$params);
	}
	
	function login(){
		if($_POST){
			$post = Qutil::filter($_POST);
			$model = new core_model_admin();
			$rs = $model->selectOne(array('adminname'=>$post['username']));
			$logmodel = new core_model_adminlog();
			if($rs['aid'] > 0){
				$pwd = md5(md5($post['pwd']).$rs['salt']);
				if($rs['adminpwd'] == $pwd){
					$t = time();
					$adminKey = md5($rs['aid'].$t.$rs['adminname'].core_lib_constant::COOKIE_KEY);
					$cookie = array('aid'=>$rs['aid'],'adminname'=>$rs['adminname'],'t'=>$t,'adminKey'=>$adminKey);
					Qutil::ssetcookie($cookie);
					$logmodel->addLog(1, "登录成功",$rs);
					$this->redirict(Qtpl::createUrl('index', 'index','','admin'));
				}
			}
			$logmodel->addLog(1, "登录失败，登录帐号：{$post['username']}");
			$this->showMsg("用户名或者密码错误",2);
		}
		$this->render('simpla/login.php');
	}
	
	function logout(){
		Qutil::ssetcookie(array('aid'=>0,'adminname'=>'','t'=>0,'adminKey'=>''),-1);
		$this->showMsg("成功退出登录",1,Qtpl::createUrl('index', 'login','','admin'));
	}
	
	function modifypwd(){
		if($this->isAdminLogin() === false){
			$this->redirict(Qtpl::createUrl('index', 'login',array(),'admin'));
		}
		if($_POST){
			$model = new core_model_admin(intval($_COOKIE['aid']));
			$rs = $model->getData();
			$opwd = md5(md5($_POST['opwd']).$rs['salt']);
			if($rs['adminpwd'] == $opwd){
				$salt = Qutil::getRandom(6,1);
				$npwd = md5(md5($_POST['npwd']).$salt);
				$model->set('adminpwd', $npwd);
				$model->set('salt', $salt);
				$model->save();
			}else{
				$this->showMsg("原密码错误",2);
			}
			$model = new core_model_adminlog();
			$model->addLog(3, "修改了密码");
			$this->logout();
		}
		$params['index_cur'] = 'current';
		$this->render('simpla/pwd.php', $params);
	}
}